package me.dengfengdecao.demo.web;

import me.dengfengdecao.demo.domain.User;
import me.dengfengdecao.demo.service.UserService;
import me.dengfengdecao.demo.utils.Const;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dengfengdecao on 16/9/26.
 */
@Controller
public class UserController {

    public static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @RequestMapping(value = "user", method = RequestMethod.POST)
    @ResponseBody
    public Object save(User user) {
        Map<String, Object> map = new HashMap<String, Object>();

        log.info("save: user <== " + user);

        user.setCreateTime(new Date());
        userService.save(user);

        map.put(Const.SUCCESS_RESPONSE, "保存成功");

        return map;
    }

    @RequestMapping(value = "user/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public Object findUserById(Model model, @PathVariable("userId")Long userId) {
        log.info("findUserById: userId : " + userId);

        User user = userService.findUserById(userId);

        return user.toString();
    }

    @RequestMapping(value = "user", method = RequestMethod.GET)
    @ResponseBody
    public String findUserByUsername(@RequestParam("username")String username){
        log.info("findUserByUsername: 执行");
        User user = userService.findByUsername(username);

        return user.toString();
    }

    @RequestMapping(value = "user2", method = RequestMethod.GET)
    @ResponseBody
    public String findUserByUsername2(@RequestParam("username")String username){
        log.info("findUserByUsername2: 执行");
        User user = userService.findByUsername(username);

        return user.toString();
    }


    @RequestMapping(value = "user/delete")
    @ResponseBody
    public String delete(Long id) {
        log.info("delete: 执行");
        userService.delFromCache(id);

        return "success";
    }

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String hello(ModelMap modelMap) {
        log.debug("hello: ");

        modelMap.addAttribute(Const.FAIL_RESPONSE, "失败");
        modelMap.put(Const.SUCCESS_RESPONSE, "保存成功");

        return "hello";
    }

    /**
     * 用户查询<br/>
     * @ RequiresPermissions 进行权限管理
     * @return
     */
    @RequestMapping("user-list")
    @RequiresPermissions("user:list")
    public String userList(ModelMap map){
        List<User> users = userService.getUsers();
        log.info("userList: users <== " + users);
        map.put("users", users);

        return "user/user-list";
    }


    /**
     * 用户添加
     * @return
    */
    @RequestMapping("user-add")
    @RequiresPermissions("user:add")
    public String userAdd(){
        log.info("userAdd: ");
        return "user/user-add";
    }

    /**
     * 用户删除
     * @return
     */
    @RequestMapping("user-del")
    @RequiresPermissions("user:del")
    public String userDelete(){
        log.info("userDelete: ");
        return "user/user-del";
    }

}
